package main.controller;

import main.model.critters.Critter;
import main.model.critters.CritterCensus;
import main.model.critters.listener.CritterLogger;
import main.model.hexisland.DefaultHexIsland;
import main.model.hexisland.HexIsland;
import main.model.hexisland.SubTileUtil;
import main.model.hexisland.listener.HexIslandLogger;
import main.model.hexisland.location.SubHexLocation;
import main.model.hexisland.location.SubHexLocationPath;
import main.userInterface.hexislandui.HexIslandCanvas;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Random;

/**
 * Created with IntelliJ IDEA.
 * User: gabe
 * Date: 22/06/13
 * Time: 22:50
 * To change this template use File | Settings | File Templates.
 */
public class IslandRunController {
    private HexIsland hexIsland;
    private CritterCensus census;
    private HexIslandCanvas canvas;

    public IslandRunController(){
        hexIsland = new DefaultHexIsland();
        hexIsland.addIslandListener(new HexIslandLogger());
        hexIsland.randomize();
        census = new CritterCensus(hexIsland);
        census.addCritterListener(new CritterLogger());
        census.generateGenesis(10);
        census.recheckLocations();
        canvas = new HexIslandCanvas(census);
    }

    public HexIslandCanvas getCanvas(){
        return canvas;
    }

    public HexIsland getHexIsland(){
        return hexIsland;
    }

    private boolean gameRunning = true;

    private int tickSpeed = 5000;
    private int refreshRate = 100; //
    private double steps = tickSpeed/refreshRate;
    double increment = (double)refreshRate/(double)tickSpeed;

    public void runLoop(){

        int count = 0;
        while(gameRunning){
            if(count > 20) break;
            findCritterLocations();
            canvas.setDelta(0);

            int step = 0;
            while(gameRunning){
                if(step > steps) break;
                canvas.setDelta(step* increment);

                try { Thread.sleep(refreshRate); } catch (Exception e) {}

                step++;
            }
            count++;
        }

    }


    public void findCritterLocations(){
        for(Critter critter : census.getCritters()){
            if(critter.isAlive()){
                ArrayList<ArrayList<SubHexLocationPath>> locations = SubTileUtil.findSubHexPathWithObstacles(
                        hexIsland ,
                        //critter.getMorphology().getSpeed()/5,
                        8,
                        critter.getLocation() ,
                        //!critter.isLargeCritter());
                        true);

                ArrayList<SubHexLocationPath> fringes = locations.get(locations.size() -1);
                Random r = new Random();
                SubHexLocationPath nextLocation = fringes.get(r.nextInt(fringes.size()));
                ArrayList<SubHexLocation> steps = new ArrayList<SubHexLocation>();
                SubHexLocationPath current = nextLocation;
                while(current.getStepFrom() != null){
                    steps.add(current);
                    current = current.getStepFrom();
                }
                steps.add(current);
                Collections.reverse(steps);
                critter.setLocation(nextLocation);
                critter.setLocationPath(steps);

                //canvas.setAccessOptions(locations);
                //try { Thread.sleep(4000); } catch (Exception e) {}
                //canvas.setAccessOptions(null);
            }
        }
        census.recheckLocations();
    }
}
